package com.collect.cameroon.zhiScene.zhiWidget.zhiFilterAdminGroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.cameroon.zhiScene.zhiCollection.zhiAdmin.ZHIAdminReduce;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDateSelect;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDay;
import com.collect.cameroon.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;

public class ZHIFilterAdminGroupMoney extends RelativeLayout {

    private final Context mContext;

    private ZHIRangeDateSelect rds_date_range;

    private ZHIRangePicker zrp_user;

    private ZHIRangePicker zrp_level;

    private ZHIFilterAdminGroupClickListener mZHIFilterAdminGroupClickListener;

    public ZHIFilterAdminGroupMoney(Context context) {
        this(context, null);
    }

    public ZHIFilterAdminGroupMoney(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterAdminGroupMoney(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        initView();
    }

    private void initView() {
        try {
            View mView = View.inflate(mContext, R.layout.view_filter_admin_group_money, this);
            rds_date_range = mView.findViewById(R.id.rds_date_range);
            zrp_user = mView.findViewById(R.id.zrp_user);
            zrp_level = mView.findViewById(R.id.zrp_level);

            TextView tv_reset = mView.findViewById(R.id.tv_reset);
            tv_reset.setOnClickListener(v -> {
                rds_date_range.reset();
                zrp_user.reset();
                zrp_level.reset();
            });

            RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
            rl_close_bg.setOnClickListener(v -> {
                if (mZHIFilterAdminGroupClickListener != null) {
                    mZHIFilterAdminGroupClickListener.onCloseClick();
                }
            });

            TextView tv_submit = mView.findViewById(R.id.tv_submit);
            tv_submit.setOnClickListener(v -> {
                if (mZHIFilterAdminGroupClickListener != null) {
                    mZHIFilterAdminGroupClickListener.onConfirmClick();
                }
            });

            zrp_user.setDataSource(initUserDataSource());
            zrp_level.setDataSource(initLevelMoneyDataSource());

            zrp_level.updateCompare(false, "B3+");
            zrp_level.mOnlyShowDefaultValue = true;

            initDueDateDataSource();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initDueDateDataSource() {
        try {
            int month = ZHICalendarUtil.getMonth();
            int cday = ZHICalendarUtil.getDay();

            String monthString = month >= 10 ? String.valueOf(month) : ("0" + month);
            String cdayString = cday >= 10 ? String.valueOf(cday) : ("0" + cday);

            String startFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + "01";
            rds_date_range.updatePromisedDueTimeStart(false, ZHICalendarUtil.getDateFromFormatString(startFormatString));

            String endFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + cdayString;
            rds_date_range.updatePromisedDueTimeEnd(false, ZHICalendarUtil.getDateFromFormatString(endFormatString));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public ArrayList<ZHIRangeDay> initUserDataSource() {
        ArrayList<ZHIRangeDay> arrayList = new ArrayList<>();
        try {
            for (int i = 0; i < ZHIAdminReduce.mGroupUserEntityArrayList.size(); i++) {
                ZHIRGroupUserEntity zhirGroupUserEntity = ZHIAdminReduce.mGroupUserEntityArrayList.get(i);
                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
                zhiRangeDay.setRangeId(i + "");
                zhiRangeDay.setDayTitle(zhirGroupUserEntity.getLoginName());
                arrayList.add(zhiRangeDay);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    public ArrayList<ZHIRangeDay> initLevelMoneyDataSource() {
        ArrayList<ZHIRangeDay> arrayList = new ArrayList<>();
        try {
            String[] levels = new String[]{"B3+"};
            for (int i = 0; i < levels.length; i++) {
                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
                zhiRangeDay.setRangeId(i + "");
                zhiRangeDay.setDayTitle(levels[i]);
                arrayList.add(zhiRangeDay);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    public String getLevel() {
        try {
            //不是传B3+,是传B+,因为分了不同的后端写，字段不一致
            return "B+";
//            String tempLevel = zrp_level.getCompare();
//            return tempLevel;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "M1";
    }

    public String getStartDate() {
        return rds_date_range.getStartDate();
    }

    public String getEndDate() {
        String tempStr = rds_date_range.getEndDate();
        tempStr = ZHICalendarUtil.getDateStringAfterDate(tempStr, 1);
        return tempStr;
    }

    public String getUser() {
        String user = zrp_user.getCompare();

        try {
            if (ZHIAdminReduce.mGroupUserEntityArrayList != null && ZHIAdminReduce.mGroupUserEntityArrayList.size() > 0) {
                for (int i = 0; i < ZHIAdminReduce.mGroupUserEntityArrayList.size(); i++) {
                    ZHIRGroupUserEntity zhirGroupUserEntity = ZHIAdminReduce.mGroupUserEntityArrayList.get(i);
                    if (zhirGroupUserEntity.getLoginName().equalsIgnoreCase(user)) {
                        user = zhirGroupUserEntity.getUserId();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }

    public void setZHIFilterAdminGroupClickListener(ZHIFilterAdminGroupClickListener clickListener) {
        this.mZHIFilterAdminGroupClickListener = clickListener;
    }
}
